//
//  ViewController.m
//  通讯录访问
//
//  Created by J on 16/3/1.
//  Copyright © 2016年 J. All rights reserved.
//

#import "ViewController.h"
#import <AddressBook/AddressBook.h>
#import <Contacts/ContactsDefines.h>
#import <Contacts/Contacts.h>
#import <ContactsUI/CNContactViewController.h>


#import <ContactsUI/CNContactPickerViewController.h>
#import <ContactsUI/CNContactViewController.h>
#import <ContactsUI/UIApplicationShortcutIcon+Contacts.h>




#define ToastShowInWindow(msg)   ToastShow([UIApplication sharedApplication].keyWindow,msg)
@interface ViewController ()
@property(nonatomic,strong)NSMutableArray* names;
@property(nonatomic,strong)NSMutableArray* phones;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // Foundation 和 Core  Foundation 相互转换. 桥接
    // 以后在使用C语言的函数时, 只要函数名称包含creat/copy/retain 就必须自己手动释放CFRelease
    // MRC 非ARC
    // 在非ARC情况下, 如果想将Foundation转换为Core  Foundation , 直接强制类型转换即可
    /*
     NSString *strOC = [NSString stringWithFormat:@"往年附加快递费"];
     CFStringRef strC = (CFStringRef)strOC;
     NSLog(@"%@ %@", strOC, strC);
     */
    
    // 在非ARC情况下, 如果想将Core Foundation转换为Foundation , 直接强制类型转换即可
    /*
     CFStringRef strC = CFStringCreateWithCString(CFAllocatorGetDefault(), "12345678", kCFStringEncodingASCII);
     NSString *strOC = ( NSString *)strC;
     NSLog(@"%@ %@", strOC, strC);
     */
    
    
    // ARC  __bridge / __bridge_retained / __bridge_transfer
    /*
     NSString *strOC = [NSString stringWithFormat:@"abcdefg"];
     // 如果使用__bridge桥接, 它仅仅是将strOC的地址给了strC, 并没有转移对象的所有权
     // 也就是说, 如果使用__bridge桥接, 那么如果strOC释放了,strC也不能用了
     // 注意: 在ARC条件下, 如果是使用__bridge桥接, 那么strC可以不用释放, 应为ARC会自动管理strOC的内容
     CFStringRef strC = (__bridge CFStringRef)strOC;
     NSLog(@"%@ %@", strOC, strC);
     */
    
    /*
     NSString *strOC = [NSString stringWithFormat:@"abcdefg"];
     // 如果使用__bridge_retained桥接, 它会将对象的所有权转移给strC, 也就是说, 即便strOC被释放了, strC也可以使用
     // 注意: 在ARC条件下,如果是使用__bridge_retained桥接, 那么strC必须自己手动释放, 因为桥接的时候已经将对象的所有权转移给了strC, 而C语言的东西不是不归ARC管理的
     //    CFStringRef strC = (__bridge_retained CFStringRef)strOC;
     CFStringRef strC = CFBridgingRetain(strOC);// 这一句, 就等同于上一句
     CFRelease(strC);
     */
    
    
    /*
     CFStringRef strC = CFStringCreateWithCString(CFAllocatorGetDefault(), "12345678", kCFStringEncodingASCII);
     // 如果使用__bridge桥接, 它仅仅是将strC的地址给了strOC, 并没有转移对象的所有权
     // 也就是说如果使用__bridge桥接, 那么如果strC释放了,strOC也不能用了
     //    NSString *strOC = (__bridge NSString *)strC;
     */
    
    /*
     CFStringRef strC = CFStringCreateWithCString(CFAllocatorGetDefault(), "12345678", kCFStringEncodingASCII);
     // 如果使用__bridge_transfer桥接, 它会将对象的所有权转移给strOC, 也就是说, 即便strC被释放了, strOC也可以使用
     // 如果使用__bridge_transfer桥接, 他会自动释放strC, 也就是以后我们不用手动释放strC
     //     NSString *strOC = (__bridge_transfer NSString *)strC;
     NSString *strOC = CFBridgingRelease(strC); // 这一句, 就等同于上一句
     */
    
//    __bridge transfers a pointer between Objective-C and Core Foundation with no transfer of ownership.
//    __bridge_retained or CFBridgingRetain casts an Objective-C pointer to a Core Foundation pointer and also transfers ownership to you.
//    You are responsible for calling CFRelease or a related function to relinquish ownership of the object.
//        
//        __bridge_transfer or CFBridgingRelease moves a non-Objective-C pointer to Objective-C and also transfers ownership to ARC.
//        ARC is responsible for relinquishing ownership of the object.
    

    
    
    ABAuthorizationStatus status =  ABAddressBookGetAuthorizationStatus();
    if (status!=kABAuthorizationStatusAuthorized) {
        
        //1.请求用户授权
        ABMultiValueRef ref = ABAddressBookCreate();
        ABAddressBookRequestAccessWithCompletion(ref, ^(bool granted, CFErrorRef error) {
            if (error) {
                NSLog(@"请求授权失败");
            }else{
                NSLog(@"请求授权成功");
                [self load];
            }
        });
    }
    
    self.names = [NSMutableArray array];
    self.phones = [NSMutableArray array];



}
- (void)load{
    //创建通讯录对象
    ABAddressBookRef address = ABAddressBookCreateWithOptions(NULL, NULL);
    //获取通讯录中所有联系人
    CFArrayRef people = ABAddressBookCopyArrayOfAllPeople(address);
    //打印每个联系人的信息
    CFIndex count = CFArrayGetCount(people);
    for (NSUInteger i= 0; i<count; i++) {
        ABRecordRef item =  CFArrayGetValueAtIndex(people, i);//联系人列表中的每个人都是一个ABRecordRef
        CFStringRef firstName = ABRecordCopyValue(item, kABPersonFirstNameProperty);
        CFStringRef lastName = ABRecordCopyValue(item, kABPersonLastNameProperty);
        
        
        ABMultiValueRef phones = ABRecordCopyValue(item, kABPersonPhoneProperty);//返回一个相当于数组
        
        CFIndex phoneCount =  ABMultiValueGetCount(phones);
        for (NSUInteger i =0; i<phoneCount; i++) {
            CFTypeRef  num =  ABMultiValueCopyValueAtIndex(phones, i);
            NSLog(@"%@,%@,%@",firstName,lastName,num);
        }
    }
    
  
    
}



@end
